<?php
//FirstName
	$boNew = false;
	$strBCC = "";
	if(!$cboTitle) {
         array_push($ActionResults, "Please enter title.");
         $intFailed++;
     }
	 
     if(!$txtFirstName) {
         array_push($ActionResults, "Please enter First Name.");
         $intFailed++;
     }else{
         $txtFirstName = ltrim(ucwords(strtolower(strip_tags($txtFirstName)))); 
     }

//Surname
     if(!$txtSurname) {
         array_push($ActionResults, "Please enter Surname.");
         $intFailed++;
     }else{
         $txtSurname = ltrim(ucwords(strtolower(strip_tags($txtSurname)))); 
     }

//Address1
     if(!$txtAddress1) {
         array_push($ActionResults, "Please enter Address1.");
         $intFailed++;
     }
	 
//Address2
     if(!$txtAddress2) {
     }else{
         $txtAddress2 = ltrim(ucwords(strtolower(strip_tags($txtAddress2)))); 
     }

//City
     if(!$txtCity) {
         array_push($ActionResults, "Please enter City.");
         $intFailed++;
     }else{
         $txtCity = ltrim(ucwords(strtolower(strip_tags($txtCity)))); 
     }

//Region
     if(!$txtRegion) {
         array_push($ActionResults, "Please enter Region.");
         $intFailed++;
     }else{
         $txtRegion = ltrim(ucwords(strtolower(strip_tags($txtRegion)))); 
     }

//PostalCode
     if(!$txtPostalCode) {
         array_push($ActionResults, "Please enter Postal Code.");
         $intFailed++;
     }else{
         $txtPostalCode = ltrim(strtoupper(strip_tags($txtPostalCode))); 
     }


     if(!$cboCountry) {
         array_push($ActionResults, "Please enter Country.");
         $intFailed++;
     }

//Phone
     if(!$txtPhone) {
         array_push($ActionResults, "Please enter Phone.");
         $intFailed++;
     }else{
         $txtPhone = ltrim(ucwords(strtolower(strip_tags($txtPhone)))); 
     }


     if(!$txtEmail) {
         array_push($ActionResults, "Please enter Email.");
         $intFailed++;
     }else{
         $txtEmail = strtolower($txtEmail); 
             if(!ereg("^([a-zA-Z0-9_\.\-]+)\@([a-zA-Z0-9\.\-]+)\.([a-zA-Z]{2,3})$", $txtEmail)){
                 array_push($ActionResults, "Is not a valid Email address");
                 array_push($ActionResults, $txtEmail);
                 $intFailed++;
             }
     }

//Password
	if(($SHOP_TYPE != "Trade") )
	{
		if(!$txtPassword) {
			array_push($ActionResults, "No Password supplied!");
			$intFailed++;
		}else{
			if($txtPassword == $txtPassword2){
			}else{
				array_push($ActionResults, "Passwords don't match!");
				$intFailed++;
			}
		}
	}
	else
	{
		if($_SESSION['sesUserType'] != "staff"){
			//Trade site and user not logged in so set random password
			$txtPassword = RandomPassword();
		}
	}
	
     if($intFailed > 0){
	 	$SCREEN = 'EnterCustomer';
         return(FALSE);
     }

     if (($intCustomerID)or($_SESSION['sesCustID'])){
	 	
		// If Customer is updating their own details
		// set $intCustomerID to $_SESSION['sesCustID'] which was set at login
		// otherwise it must be a member of staff updating a customer
		if(($_SESSION['sesUserType'] == "customer") OR (!$_SESSION['sesUserType'] == "staff")){
			$intCustomerID = $_SESSION['sesCustID'];
			$strConfirmation = "Your details have been updated successfully";
		}else{
			$strConfirmation = "Customer ".$txtEmail." updated successfully";
		}
		
        $strSQL = "Update tblCustomer
                     SET   tblCustomer.Title =  '$cboTitle'
                         , tblCustomer.FirstName =  '$txtFirstName'
                         , tblCustomer.Surname =  '$txtSurname'
                         , tblCustomer.Address1 =  '$txtAddress1'
                         , tblCustomer.Address2 =  '$txtAddress2'
                         , tblCustomer.City =  '$txtCity'
                         , tblCustomer.Region =  '$txtRegion'
                         , tblCustomer.PostalCode =  '$txtPostalCode'
                         , tblCustomer.Country =  '$cboCountry'
                         , tblCustomer.Phone =  '$txtPhone'
						 , tblCustomer.VATNumber = '$txtVATNumber'
						 , tblCustomer.ValidFor = '$cboValidFor'
						 , tblCustomer.CurrencyProfile = '$cboCurrencyProfile'
                         , tblCustomer.Fax =  '$txtFax'
						 , tblCustomer.Actual_FAX = '$txtActual_FAX'
                         , tblCustomer.Email =  '$txtEmail'
                         , tblCustomer.Password =  password('$txtPassword')
                         , tblCustomer.Referrer =  '$cboReferrer'
						 , tblCustomer.OtherInfo =  '$txtOtherInfo'
     "; 
        $strJoin = " WHERE ";
        $strSQL .= $strJoin."tblCustomer.ID =  '$intCustomerID'";
        $strMessage = " Updated";
		
	 	$subject = SERVER_NAME." Updated Client Details";
		$msgbody = "<link REL=stylesheet HREF='http://$SERVER_NAME/shop".SITE_SUFFIX.".css'>
			<table width='600' align='center' class='Normal'>
				<tr>
					<td colspan='2'>
						Dear $txtFirstName
						<br>
						<br>Your details have been updated.
						<br>
						<br>Regards
						<br>Customer Service Team
						<br>
						<br>
					</td>
				
				</tr>";
				include (MODULE . "/screen/EmailCustomerDetails");
				echo "
			</table>";

		
    }Else{
		$boNew = true;
        $strSQL = "INSERT INTO tblCustomer
                     (     tblCustomer.Title
                         , tblCustomer.FirstName
                         , tblCustomer.Surname
                         , tblCustomer.Address1
                         , tblCustomer.Address2
                         , tblCustomer.City
                         , tblCustomer.Region
                         , tblCustomer.PostalCode
                         , tblCustomer.Country
						 , tblCustomer.VATNumber
						 , tblCustomer.ValidFor 
						 , tblCustomer.CurrencyProfile
                         , tblCustomer.Phone
                         , tblCustomer.Fax
						 , tblCustomer.Actual_FAX
                         , tblCustomer.Email
                         , tblCustomer.Password
                         , tblCustomer.RegistrationDate
                         , tblCustomer.Referrer
                         , tblCustomer.LoginDate
						 , tblCustomer.OtherInfo
                     )Values( '$cboTitle'
                         , '$txtFirstName'
                         , '$txtSurname'
                         , '$txtAddress1'
                         , '$txtAddress2'
                         , '$txtCity'
                         , '$txtRegion'
                         , '$txtPostalCode'
                         , '$cboCountry'
						 , '$txtVATNumber'
						 , '$cboValidFor'
						 , '$cboCurrencyProfile'
                         , '$txtPhone'
                         , '$txtFax'
						 , '$txtActual_FAX'
                         , '$txtEmail'
                         , password('$txtPassword')
                         , now()
                         , '$cboReferrer'
                         , now()
						 , '$txtOtherInfo'
                       )";
        //$strMessage = "added";
		//  $strMessage = $txtFirstName." ".$txtSurname." Thankyou For Registering with ".SITE_NAME."<br>
		//To order simply log in using your email address and password.";
		

		$subject = "Thank you for registering with ".SERVER_NAME;
		
		if($SHOP_TYPE == "Trade")
		{
			$msgbody = "<link rel=stylesheet href='http://$SERVER_NAME/shop".SITE_SUFFIX.".css'>
			<table width='600' align='center' class='Normal'>
				<tr>
					<td colspan='2'>
						Dear $txtFirstName
						<br><br> 
						You have been successfully registered with ".SERVER_NAME.". You will be contacted shortly with your password.
						<br>
						<br>Regards
						<br>Customer Service Team
						<br>
						<br>
					</td>
				
				</tr>";
			
		}
		else
		{
			$msgbody = "<link REL=stylesheet HREF='http://$SERVER_NAME/shop".SITE_SUFFIX.".css'>
			<table width='600' align='center' class='Normal'>
				<tr>
					<td colspan='2'>
						Dear $txtFirstName
						<br><br> 
						You have been successfully registered with ".SERVER_NAME.". If you wish to change your details please login 
						using $txtEmail and go to the 'Customer' menu and select 'Update My Details'.
						<br>
						<br>Regards
						<br>Customer Service Team
						<br>
						<br>
					</td>
				
				</tr>";

		}	
						include (MODULE . "/screen/EmailCustomerDetails");
				echo "
			</table>";
				
		
		
		// If Customer is updting their own details
		// set $intCustomerID to $_SESSION['sesCustID'] which was set at login
		// otherwise it must be a member of staff updating a customer
		if(($_SESSION['sesUserType'] == "customer") OR (!$_SESSION['sesUserType'] == "staff")){
			$strConfirmation = "Thank you for registering $txtFirstName. You are now logged in.";
		}else{
			$strConfirmation = "$txtEmail has now been registered.";
		}
	 	
		
		
		
    }
	$strSQL .= ";";
	
	include (MODULE_SHOP_HOST . "/navigation/EmailHeader");
	
	  if (!include (MODULE . "/action/actMySqlResults"))
	  {
	      $strConfirmation = "Registration failed. Have you registered before?";
	  }
	  else
	  {
	  	$intNewCustomerID = mysql_insert_id();
		if(($SHOP_TYPE == "Trade") && ($_SESSION['sesUserType'] != "staff"))
		{
			//New trade customer so send them to Trade welcome page
			$msgbody = "<link rel=stylesheet href='http://$SERVER_NAME/shop".SITE_SUFFIX.".css'>
			<table width='600' align='center' class='Normal'>
				<tr>
					<td colspan='2'>
						Dear $txtFirstName
						<br><br> 
						You have been successfully registered with ".SERVER_NAME.". You will be contacted shortly with your password.
						<br>
						<br>Regards
						<br>Customer Service Team
						<br>
						<br>
					</td>
				
				</tr>";
			$subject = "Thank you for registering with ".SERVER_NAME;
			$strFromEmail = "noreply@".WEB_DOMAIN;
			$strFromName = "User Admin ".SITE_NAME;
			$SCREEN = "NewTrade";
			$strToEmail = $txtEmail;

			SendMime($subject, $msgbody, $strToEmail, $strFromEmail, $strFromName,$strHeader);
			$msgbody = "<link rel=stylesheet href='http://$SERVER_NAME/shop".SITE_SUFFIX.".css'>
			<table width='600' align='center' class='Normal'>
				<tr>
					<td colspan='2'>
						$txtFirstName $txtSurname ($strToEmail) has been registered and will need their password setting. 
						<br>
					</td>
				
				</tr>";
			//echo "EMAIL_ORDER ".EMAIL_ORDER;
			$subject = $intNewCustomerID." New registration $txtFirstName $txtSurname ($strToEmail)";
			SendMime($subject, $msgbody, $strEmailBCC, $strFromEmail, $strFromName,$strHeader);
			$strConfirmation = "Thank you for registering $txtFirstName. You will be contacted shortly with your password.";
		}
		else
		{
	  		//If not a new trade customer login
			$strFromEmail = "noreply@".WEB_DOMAIN;
			$strFromName = "User Admin ".SITE_NAME;

				if($_SESSION['sesUserType'] == "staff"){ 
				
					$strToEmail = $txtEmail;
					SendMime($subject, $msgbody, $strToEmail, $strFromEmail, $strFromName,$strHeader);
				}else{
						//echo "not staff<br>";
						if (!$_SESSION['sesCustID']){
							$_SESSION['sesCustID'] = mysql_insert_id();
							//echo $_SESSION['sesCustID']." cust id<br>";
						}
					
					session_register(sesCustID);
					unset($intCustomerID);
					$_SESSION['sesUserName'] = $txtEmail;
					session_register(sesUserName);
					$_SESSION['sesUserType'] = "customer";
					session_register(sesUserType);
					
					array_push($ActionResults, $strConfirmation);

					$strToEmail = $_SESSION['sesUserName'];
					SendMime($subject, $msgbody, $strToEmail, $strFromEmail, $strFromName,$strHeader);
					unset($strReprocessing);
				
		
				}
				// Snd to Welcome screen on successful login
				//$SCREEN = 'DisplayOwnDetails';
		}
	}
	
	array_push($ActionResults, $strConfirmation);
?>
